<?php
	include('../inc/conn.php');
	include('../inc/fnc.php');

	if(isset($_POST)){
		if (!check_email_address($_POST['email'])) {
			$error['type']= 'invalid';					//invalid email provided
		}  else {
			$query = "SELECT * FROM `invites` WHERE `email` = '{$_POST['email']}' LIMIT 1";
			$res = mysql_query($query);
			if (mysql_num_rows($res) == 1) {
				$error['type']= 'exist';				//Email already registered for code
			} else  {		
				$icode =  genRandomString(10);
				$res = mysql_query("insert into invites values('','{$_POST['email']}','{$icode}', '0', NOW(), 'N')") or die(mysql_error());
				if($res){
					$error['type']= 'success';			//key successfully generated
				} else {
					$error['type']= 'error';			// Key generation failed
				} //if($res) ends
			}	//if (mysql_num_rows($res) == 1) Ends
		}	// if (!check_email_address($_POST['email'])) ENDs
	}	//if(isset($_POST)) Ends
	
	echo json_encode($error);
?>
